package com.oanda.blackberry.view;

import com.oanda.blackberry.net.UpgradeCheck;
import com.oanda.blackberry.ui.BitmapCheckboxField;
import com.oanda.blackberry.ui.CustomObjectChoiceField;
import com.oanda.blackberry.ui.OandaMainScreen;
import com.oanda.blackberry.util.AbstractViewPanel;
import com.oanda.blackberry.util.DefaultController;
import com.oanda.blackberry.util.Model;
import com.oanda.blackberry.util.PropertyChangeEvent;
import com.oanda.blackberry.util.Settings;
import net.rim.blackberry.api.browser.Browser;
import net.rim.blackberry.api.browser.BrowserSession;
import net.rim.device.api.system.Application;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.Screen;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.CheckboxField;
import net.rim.device.api.ui.component.ChoiceField;
import net.rim.device.api.ui.component.Menu;

public class OptionsView implements AbstractViewPanel {
	DefaultController controller;
	private static String[] options = { "Auto", "BIS-B", "WAP2", "Direct TCP",
			"MDS(BES)", "Default" };

	BitmapCheckboxField trustAll = new BitmapCheckboxField(
			"Trust All Secure Connections", false, 18014398509481984L);
	BitmapCheckboxField enableHttpsRedirect = new BitmapCheckboxField(
			"Allow HTPPS Redirection", false, 18014398509481984L);
	BitmapCheckboxField endToEndRequired = new BitmapCheckboxField(
			"Preffer TSL HandHeld Mode", false, 18014398509481984L);

	CustomObjectChoiceField ccf = new CustomObjectChoiceField(16119285, 0,
			"Connection Options: ", options, 450, 30);

	ButtonField upgrade = new ButtonField("Check for Upgrade", 65536L);

	OandaMainScreen screen = new OandaMainScreen() {
		protected void makeMenu(Menu menu, int instance) {
			menu.add(OptionsView.this.backMenuItem);
		}
	};

	MenuItem backMenuItem = new MenuItem("Back", 10, 1001) {
		public void run() {
			if (OptionsView.this.screen != null) {
				OptionsView.this.controller.popScreen(OptionsView.this.screen);
				OptionsView.this.screen.onClose();
			}
			OptionsView.this.screen = null;
		}
	};

	public OptionsView(DefaultController controller) {
		this.controller = controller;

		this.ccf.setChangeListener(new FieldChangeListener() {

			public void fieldChanged(Field field, int context) {
				OptionsView.this.controller
						.setConnectionSuffixIndex(OptionsView.this.ccf
								.getSelectedIndex());
			}
		});
		this.ccf.setTextFont(DefaultController.getComponentFont());

		this.trustAll.setChangeListener(new FieldChangeListener() {

			public void fieldChanged(Field field, int context) {
				OptionsView.this.controller.trustAll(OptionsView.this.trustAll
						.getChecked());
			}
		});
		this.enableHttpsRedirect.setChangeListener(new FieldChangeListener() {

			public void fieldChanged(Field field, int context) {
				OptionsView.this.controller
						.enableHttpsRedirect(OptionsView.this.enableHttpsRedirect
								.getChecked());
			}
		});
		this.endToEndRequired.setChangeListener(new FieldChangeListener() {
			// private final DefaultController val$controller;

			public void fieldChanged(Field field, int context) {
				OptionsView.this.controller
						.endToEndRequired(OptionsView.this.endToEndRequired
								.getChecked());
			}
		});
		this.upgrade.setChangeListener(new FieldChangeListener() {
			public void fieldChanged(Field field, int context) {
				OptionsView.this.updateSequence();
			}
		});
	}

	private void init() {
		this.screen = new OandaMainScreen() {
			protected void makeMenu(Menu menu, int instance) {
				menu.add(OptionsView.this.backMenuItem);
			}
		};
		this.screen.updateTitle("Options");

		this.screen.add(this.ccf);

		this.screen.add(this.trustAll);
		this.screen.add(this.enableHttpsRedirect);
		this.screen.add(this.endToEndRequired);

		this.screen.add(this.upgrade);
	}

	public void setTrustAll(final boolean checked) {
		UiApplication.getUiApplication().invokeLater(new Runnable() {
			private final boolean _checked = checked;

			public void run() {
				OptionsView.this.trustAll.setChecked(this._checked);
			}
		});
	}

	public void setEndToEndRequired(final boolean checked) {
		UiApplication.getUiApplication().invokeLater(new Runnable() {
			private final boolean _checked = checked;

			public void run() {
				OptionsView.this.endToEndRequired.setChecked(this._checked);
			}
		});
	}

	public void setEnableHttpsRedirect(final boolean checked) {
		UiApplication.getUiApplication().invokeLater(new Runnable() {
			private final boolean _checked=checked;

			public void run() {
				OptionsView.this.enableHttpsRedirect.setChecked(this._checked);
			}
		});
	}

	public void setConnectionChoicesIndex(final int index) {
		UiApplication.getUiApplication().invokeLater(new Runnable() {
			private final int _index=index;

			public void run() {
				if ((this._index < OptionsView.options.length)
						&& (this._index > -1))
					OptionsView.this.ccf.setSelectedIndex(this._index);
			}
		});
	}

	public void modelPropertyChange(PropertyChangeEvent evt) {
		if (evt.getPropertyName().equals("setConnectionSuffixIndex")) {
			setConnectionChoicesIndex(((Integer) evt.getNewValue()).intValue());
		} else if (evt.getPropertyName().equals("setSettings")) {
			setConnectionChoicesIndex(((Settings) evt.getNewValue())
					.getConnectionSuffixIndex());
			setTrustAll(((Settings) evt.getNewValue()).isTrustAll());
			setEnableHttpsRedirect(((Settings) evt.getNewValue())
					.isEnableHttpsRedirec());
			setEndToEndRequired(((Settings) evt.getNewValue())
					.isEndToEndRequired());
		} else if (evt.getPropertyName().equals("showOptionsView")) {
			init();
			this.controller.pushScreen(this.screen);
		}
	}

	private void onCloseEvent() {
		if (this.screen != null)
			this.screen.deleteAll();
		this.screen = null;
	}

	public void updateSequence() {
		if (this.upgrade.getLabel().equals("Upgrade")) {
			BrowserSession session = Browser.getDefaultSession();
			session.displayPage("http://m.oanda.com/rim/fxtrade/download");
		}
		if (this.upgrade.getLabel().equals("Check for Upgrade")) {
			UpgradeCheck uc = new UpgradeCheck();
			String result = uc
					.proceedConnection("http://m.oanda.com/rim/fxtrade/upgrade?ver="
							+ Model.getVERSION());

			if (!result.startsWith("latest")) {
				this.upgrade.setLabel("Upgrade");
				DefaultController.showMessage("An upgrade has been found.");
			} else {
				DefaultController
						.showMessage("No upgrade found. You are currently running the latest version of fxTrade.");
			}
		}
	}
}
